import pygame
import stackless

import time

from pygame.locals import *

from pos import Position

import items,charectors

from actor import Actor
from log import getLogger

logger=getLogger('display')

import sys


class Display(Actor):

    def __init__(self,fout=sys.stdout,size=(100,100),unitsize=(1,1)):

        Actor.__init__(self)

        self.fout=fout
        self.size=size
        self.unitsize=unitsize

#TODO rename fout

class _Display(Display):
    def update(self,positions):
        pass
    
    def stage_clear(*args):
        pass

class TextDisplay(Display):

    def update(self,positions):

        for x in range(self.size[0]):
            for y in range(self.size[1]):

                pos=Position(x,y)
                if pos in self.world.map_:
                    try :
                        char=list(self.world.map_[pos])[0]
                    except Exception as e:
                        logger.debug(pos)
                        raise e

                    if isinstance(char,items.Bomb):
                        c='!'

                    elif isinstance(char,items.Gold):
                        c='$'

                    elif isinstance(char,charectors.Charector):
                        c='*'

                    elif isinstance(char,items.DestroyableWall):
                        c='X'

                    elif isinstance(char,items.UnDestroyableWall):
                        c='#'
                    elif isinstance(char,items.Explotion):
                        c='%'
                    elif isinstance(char,items.Exit):
                        c='^'

                    print c,
                else :
                    print '.',

            print '---'

        print '---------------------------------------------------'
                    
    def stage_clear(self):
        print 'stage_clear'

        raise SystemExit

    def lose(self):
        print 'lose'

        raise SystemExit

class  ChannelDisplay(Display):

    def update(self,positions):

        self.fout.send(postions)

class PyGameDisplay(Display):

    def lose(self):
        font=pygame.font.Font(None,36)
        text=font.render('You Lose!',1,(10,10,10))
        textpos=text.get_rect(centerx=self.size[0]*30/2)
        self.screen.blit(text,textpos)

        pygame.display.flip()
        time.sleep(5)

        raise SystemExit


    def stage_clear(self):
        font=pygame.font.Font(None,36)
        text=font.render('You Win!',1,(10,10,10))
        textpos=text.get_rect(centerx=self.size[0]*30/2)
        self.screen.blit(text,textpos)

        pygame.display.flip()
        time.sleep(5)

        raise SystemExit

    def load(self,name):
        image=pygame.image.load(name)
        image=image.convert()

        image.set_colorkey(image.get_at((0,0)))

        return image

    def update(self,*args,**kwargs):

        if not hasattr(self,'inited'):

            self.screen=pygame.display.set_mode((self.size[0]*30,self.size[1]*30))
            
            self.background=pygame.Surface(self.screen.get_size())
            self.background=self.background.convert()
            self.background.fill((0,0,255))

            self.bomb=self.load('bomb.jpg')
            self.dollars=self.load('dollars.png')
            self.man=self.load('images.jpg')
            self.DestroyableWall=self.load('destroyableWall.jpg')
            self.UndestroyableWall=self.load('UnDestroyableWall.jpg')
            self.explotion=self.load('explotion.jpg')
            self.door=self.load('door.jpg')


            self.screen.blit(self.background,(0,0))

            self.inited=True

        for x in range(self.size[0]):
            for y in range(self.size[1]):

                pos=Position(x,y)
                if pos in self.world.map_:
                    try :
                        char=list(self.world.map_[pos])[0]
                    except Exception as e:
                        logger.debug(pos)
                        raise e

                    if isinstance(char,items.Bomb):
                        c=self.bomb

                    elif isinstance(char,items.Gold):
                        c=self.dollars

                    elif isinstance(char,charectors.Charector):
                        c=self.man

                    elif isinstance(char,items.DestroyableWall):
                        c=self.DestroyableWall

                    elif isinstance(char,items.UnDestroyableWall):
                        c=self.UndestroyableWall

                    elif isinstance(char,items.Explotion):
                        c=self.explotion

                    elif isinstance(char,items.Exit):
                        c=self.door

                    self.screen.blit(c,(x*30,y*30))
                else :
                    self.screen.blit(self.background,(x*30,y*30))

            pygame.display.flip()
